package com.seed.core.query;

import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryTable;
import com.seed.core.column.Column;
import com.seed.core.enumeration.PropertyType;
import com.seed.core.pojo.Join;
import com.seed.core.pojo.seed.HasManys;
import com.seed.core.pojo.seed.Relation;
import com.seed.core.pojo.seed.Seed;
import com.seed.core.query.pojo.Declared;
import com.seed.util.StrKit;
import com.seed.util.SeedKit;

import java.util.LinkedHashMap;
import java.util.Map;

public class SeedDeclared {
    private final Map<String, QueryColumn> selects = new LinkedHashMap<>(); // 选择列对象
    private final Map<String, Join> leftJoins = new LinkedHashMap<>(); // 表关联对象
    private final Map<String, SeedFill> fills = new LinkedHashMap<>(); // 数据填充对象

    public SeedDeclared(Seed selfSeed, Map<String, Declared> declared) {
        init(null, selfSeed, declared);
    }

    private void init(String basePath, Seed selfSeed, Map<String, Declared> declared) {
        String selfTableAlias = StrKit.selfTableAlias(basePath);
        QueryTable selfTable = new QueryTable(selfSeed.getTableName()).as(selfTableAlias);
        for(Map.Entry<String, Declared> entry: declared.entrySet()) {
            String crossOrColumn = entry.getKey();
            String finalPath = StrKit.concat(basePath, crossOrColumn);
            Declared value = entry.getValue();
            PropertyType propertyType = value.getType();
            switch (propertyType) {
                case none, asterisk -> {
                    Map<String, Column> columns = selfSeed.getColumns();
                    for (Column column: columns.values()) {
                        String columName = column.getName();
                        String finalColumn = StrKit.concat(basePath, columName);
                        String columnAlias = StrUtil.toCamelCase(finalColumn);
                        selects.putIfAbsent(finalColumn, new QueryColumn(selfTable, columName, columnAlias));
                    }
                }
                case column -> {
                    String columnAlias = StrUtil.toCamelCase(finalPath);
                    selects.putIfAbsent(finalPath, new QueryColumn(selfTable, crossOrColumn, columnAlias));
                }
                case hasOne, belongsTo -> {
                    Relation relation = (Relation) value.getProperty();
                    Seed targetSeed = relation.targetSeed();
                    String targetTableAlias = StrUtil.toCamelCase(finalPath);
                    QueryTable targetTable = new QueryTable(targetSeed.getTableName()).as(targetTableAlias);

                    QueryCondition on = relation.getQueryCondition(basePath, StrUtil.toCamelCase(crossOrColumn));
                    leftJoins.putIfAbsent(finalPath, new Join(targetTable, on));
                    init(finalPath, targetSeed, value);
                }
                default -> {
                    Relation relation = (Relation) value.getProperty();
                    Seed targetSeed = relation.targetSeed();
                    for(Map.Entry<String, QueryColumn> e: relation.getSelfExtraSelects(basePath).entrySet()) {
                        selects.putIfAbsent(entry.getKey(), e.getValue());
                    }
                    fills.put(finalPath, new SeedFill(basePath, crossOrColumn, (HasManys) relation, targetSeed, value));
                }
            }
        }
    }

    public Map<String, QueryColumn> getSelects() {
        return selects;
    }

    public Map<String, Join> getLeftJoins() {
        return leftJoins;
    }

    public Map<String, SeedFill> getFills() {
        return fills;
    }
}
